package com.invengo.module_check.ui.fragment.book_opeartion_result;

import android.app.Application;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.entity.ActionArgs;
import com.invengo.module_check.ui.fragment.book_opeartion_result.item.BookOpeartionResultItemViewModel;
import com.invengo.module_check.ui.fragment.book_opeartion_result.item.ShelfOpeartionResultItemViewModel;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class BookOpeartionResultViewModel extends BaseViewModel {
    //    public SingleLiveEvent<Boolean> opeartionType =new SingleLiveEvent<>();
    public SingleLiveEvent<Boolean> opeartionState = new SingleLiveEvent<>();
    public SingleLiveEvent<String> opeartiontitle = new SingleLiveEvent<>();

    public SingleLiveEvent<String> opeartionTip = new SingleLiveEvent<>();
    public ObservableList<ShelfOpeartionResultItemViewModel> shelfList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<ShelfOpeartionResultItemViewModel> shelfitemBinding = ItemBinding.of(BR.viewModel, R.layout.item_shelf_opeartion_result);

    public ObservableList<BookOpeartionResultItemViewModel> bookList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<BookOpeartionResultItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_book_opeartion_result);

    public BookOpeartionResultViewModel(@NonNull Application application) {
        super(application);
    }


    public UIChangeObservable ui = new UIChangeObservable();

    public int getPosition(ItemViewModel item) {
        switch (actionArgs) {
            case ACTION_FIRST:
                return shelfList.indexOf(item);
            default:
                return bookList.indexOf(item);
        }
    }


    public class UIChangeObservable {
        public SingleLiveEvent<Void> lanchMainEvent;
        public SingleLiveEvent<Void> back2workEvent;

        public UIChangeObservable() {
            lanchMainEvent = new SingleLiveEvent<>();
            back2workEvent = new SingleLiveEvent<>();
        }
    }

    private ActionArgs actionArgs;


    public void addBookList(ActionArgs actionArgs, List<BookBean> bookBeans, List<ShelfBean> shelfBeans, boolean opState) {
        this.actionArgs = actionArgs;
        switch (actionArgs) {
            case ACTION_LISTING:
            case ACTION_FORCE:
            case ACTION_REVOKE:
            case ACTION_INVENTORY:
            case ACTION_PARALLEL:
            case ACTION_CHANGE:
            case ACTION_DOWN:
                opeartionState.postValue(opState);
                opeartiontitle.postValue("操作" + (opState ? "成功" : "失败"));
                opeartionTip.postValue(opState ? "完成以下书籍的" + actionArgs.getName() + "操作" : actionArgs.getName() + "操作失败，请重新处理！");
                if (bookBeans != null) {
                    for (BookBean bookBean : bookBeans) {
                        bookList.add(new BookOpeartionResultItemViewModel(this, bookBean));
                    }
                }
                break;
            case ACTION_FIRST:
                opeartionState.postValue(opState);
                opeartiontitle.postValue("操作" + (opState ? "成功" : "失败"));
                opeartionTip.postValue(opState ? "完成以下层架的" + actionArgs.getName() + "操作" : actionArgs.getName() + "操作失败，请重新处理！");
                if (shelfBeans != null) {
                    for (ShelfBean shelfBean : shelfBeans) {
                        shelfList.add(new ShelfOpeartionResultItemViewModel(this, shelfBean));
                    }
                }
                break;
        }

    }

    /**
     * 后台工作
     */
    public BindingCommand back2work = new BindingCommand(() -> {
        ui.back2workEvent.call();
    });
    /**
     * 下水总管
     */
    public BindingCommand launchMain = new BindingCommand(() -> {
        ui.lanchMainEvent.call();
    });

}
